﻿package net.wangit.context;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 本地线程上下文，每次URL请求时创建，持有ActionContext对象，
 * 可以在程序的任何位置通过LocalContext.getLocalContext()获取实例。
 * 
 * @author watson Feb 25, 2010
 */
public class LocalContext {

	private static ThreadLocal<LocalContext> local = new ThreadLocal<LocalContext>();

	// 取得LocalContext实例。
	public static LocalContext getLocalContext() throws LocalContextException {
		LocalContext lc = local.get();
		if (lc == null)
			throw new LocalContextException("LocalContext is null!");
		else
			return lc;
	}

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletConfig servletConfig;

	private ActionContext actionContext;

	protected LocalContext() {
		local.set(this);
	}

	// 取得ActionContext。
	public ActionContext getActionContext() {
		return actionContext;
	}

	// 取得HttpServletRequest。
	public HttpServletRequest getHttpServletRequest() {
		return request;
	}

	// 取得HttpServletResponse。
	public HttpServletResponse getHttpServletResponse() {
		return response;
	}

	// 取得ServletConfig。
	public ServletConfig getServletConfig() {
		return servletConfig;
	}

	// 设置ActionContext。
	protected void setActionContext(ActionContext ac) {
		actionContext = ac;
	}

	// 设置HttpServletRequest。
	protected void setHttpServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	// 设置HttpServletResponse。
	protected void setHttpServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	// 设置ServletConfig。
	protected void setServletConfig(ServletConfig servletConfig) {
		this.servletConfig = servletConfig;
	}

}
